package com.example.jiuzhan_client;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


import com.example.server.IStudentChangeListener;
import com.example.server.IStudentService;
import com.example.server.Student;



public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView mTvCount;
    private EditText mEtName;
    private EditText mEtNumber;
    private EditText mEtGrade;
    private EditText mEtAge;
    private EditText mEtRmNumber;
    private Button mBtnAdd;
    private Button mBtnRemove;

    private TextView mName,mNumber,mGrade,mAge;
    private EditText mSelect;
    private Button mBtnSelect;
    private IStudentService mService;
    private int mStudentCount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTvCount = findViewById(R.id.count);
        mEtName = findViewById(R.id.name);
        mEtNumber = findViewById(R.id.number);
        mEtGrade = findViewById(R.id.grade);
        mEtAge = findViewById(R.id.age);
        mEtRmNumber = findViewById(R.id.rm_number);
        mBtnAdd = findViewById(R.id.add);
        mBtnRemove = findViewById(R.id.remove);

        mName = findViewById(R.id.name2);
        mNumber = findViewById(R.id.number2);
        mGrade = findViewById(R.id.grade2);
        mAge = findViewById(R.id.age2);
        mSelect = findViewById(R.id.select);
        mBtnSelect = findViewById(R.id.btn_select);

        mTvCount.setOnClickListener(this);
        mBtnAdd.setOnClickListener(this);
        mBtnRemove.setOnClickListener(this);
        mBtnSelect.setOnClickListener(this);
        bindService();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(mConnection);
    }

    private void bindService() {
        Intent intent = new Intent();
        intent.setPackage("com.example.server");
        intent.setAction("com.example.server.action");
        startService(intent);
        bindService(intent, mConnection, BIND_AUTO_CREATE);
    }

    private ServiceConnection mConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mService = IStudentService.Stub.asInterface(service);
            try {
                mService.addListener(mListener);
                mStudentCount = mService.getStudentCount();
                mTvCount.setText(mStudentCount + "");
            } catch (RemoteException e) {
                throw new RuntimeException(e);
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            mService = null;
        }
    };

    IStudentChangeListener mListener = new IStudentChangeListener.Stub() {
        @Override
        public void onStudentCountChanged(int count) throws RemoteException {
            mStudentCount = count;
            mTvCount.setText(mStudentCount + "");
        }
    };

    @Override
    public void onClick(View v) {
        if (mService == null) {
            return;
        }
        int id = v.getId();
        if (id == R.id.add) {
            try {
                String name = mEtName.getText().toString();
                int number = Integer.parseInt(mEtNumber.getText().toString());
                String grade = mEtGrade.getText().toString();
                int age = Integer.parseInt(mEtAge.getText().toString());
                Student student = new Student(name, number, grade, age);
                mService.addStudent(student);
            } catch (RemoteException e) {
                throw new RuntimeException(e);
            }

        } else if (id == R.id.remove) {
            try {
                int rm_number = Integer.parseInt(mEtRmNumber.getText().toString());
                mService.deleteStudent(rm_number);
            } catch (RemoteException e) {
                throw new RuntimeException(e);
            }
        } else if (id == R.id.count) {
            try {
                mTvCount.setText(mService.getStudentCount() + "");
            } catch (RemoteException e) {
                throw new RuntimeException(e);
            }
        }else if (id == R.id.btn_select) {
            // 按 number 查询学生信息的逻辑
            try {
                String mEtNumber = mSelect.getText().toString();
                if (!mEtNumber.isEmpty()) {
                    int number = Integer.parseInt(mEtNumber);
                    Student student = mService.getStudentByNumber(number);
                    if (student != null) {
                        // 如果查询到学生信息，则更新 UI
                        updateStudentInfo(student);
                    } else {
                        // 如果没有查询到学生信息，显示提示
                        Toast.makeText(this, "没有找到学生信息", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(this, "请输入学生编号", Toast.LENGTH_SHORT).show();
                }
            } catch (NumberFormatException e) {
                Toast.makeText(this, "请输入有效的学生编号", Toast.LENGTH_SHORT).show();
            } catch (RemoteException e) {
                Toast.makeText(this, "服务调用失败", Toast.LENGTH_SHORT).show();
            }
        }
    }
    private void updateStudentInfo(Student student) {
        // 更新 UI 显示学生信息
        mName.setText(student.getName());
        mNumber.setText(String.valueOf(student.getNumber()));
        mGrade.setText(student.getGrade());
        mAge.setText(String.valueOf(student.getAge()));
    }
}